package com.teste.cadastrodeclientes;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
	// Inflate the menu; this adds items to the action bar if it is present.
	getMenuInflater().inflate(R.menu.main, menu);
	return true;
    }
    public void cadastrarCliente(View v){
	EditText txtNome = (EditText) findViewById(R.id.txtNome);
	EditText txtEmail = (EditText) findViewById(R.id.txtEmail);
	EditText txtCidade = (EditText) findViewById(R.id.txtCidade);
	
	if(txtNome.getText().toString().length() <= 0){
	    txtNome.setError("Preencha o campo nome.");
	    txtNome.requestFocus();
	}
	if(txtEmail.getText().toString().length() <= 0){
	    txtEmail.setError("Preencha o campo email.");
	    txtEmail.requestFocus();
	}
	if(txtCidade.getText().toString().length() <= 0){
	    txtCidade.setError("Preencha o campo cidade.");
	    txtCidade.requestFocus();
	}
	
	SQLiteDatabase db = openOrCreateDatabase("clientes.db", Context.MODE_PRIVATE, null);
	
	ContentValues ctv = new ContentValues();
	ctv.put("nome", txtNome.getText().toString());
	ctv.put("email", txtEmail.getText().toString());
	ctv.put("cidade", txtCidade.getText().toString());
	
	if(db.insert("clientes.db", "_id", ctv) > 0){
	    Toast.makeText(getBaseContext(), "Sucesso ao cadastrar", Toast.LENGTH_SHORT).show();
	}else {
	    Toast.makeText(getBaseContext(), "Erro ao cadastrar", Toast.LENGTH_SHORT).show();
	    
	}
	
	
    }

}
